package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.CustomerReturnListService;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author SZ
 * @date 2022/9/26/9:44
 */
@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {
    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;
    @Autowired
    private CustomerReturnListService customerReturnListService;
    @Autowired
    private GoodsService goodsService;

    @Override
    public ServiceVO save(CustomerReturnList customerReturnList, List<CustomerReturnListGoods> customerReturnListGoodsList) {
        //增加一条记录到表:t_customer_return_list,需要拿到自增主键
        customerReturnListService.save (customerReturnList);
        //增加若干记录到表:t_customer_return_list_goods
        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {
            //设置没有的字段
            customerReturnListGoods.setCustomerReturnListId (customerReturnList.getCustomerReturnListId ());
            customerReturnListGoodsDao.save (customerReturnListGoods);
            //增加库存到表:t_goods
            Integer goodsId = customerReturnListGoods.getGoodsId ();
            Integer goodsNum = customerReturnListGoods.getGoodsNum ();
            goodsService.addQuantity (goodsNum, goodsId);
        }

        return new ServiceVO (SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<CustomerReturnListGoods> getGoodsList(Integer customerReturnListId) {
        if (customerReturnListId != null){
            return customerReturnListGoodsDao.getGoodsList(customerReturnListId);
        }
         return null;
    }

    @Override
    @Transactional
    public ServiceVO delete(Integer customerReturnListId) {
        //1-删除若干记录在表:t_customer_return_list_goods(首先得做这步，不然第二步会因为主外键关联无法执行)
        customerReturnListGoodsDao.deleteById(customerReturnListId);
        //2-删除一条记录在表:t_customer_return_list
        customerReturnListService.deleteById(customerReturnListId);
        return null;
    }
}
